package com.studyhub.question.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.studyhub.question.model.entity.QueMailboxEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 消息通知数据访问层
 */
@Mapper
public interface MailboxMapper extends BaseMapper<QueMailboxEntity> {

    /**
     * 查询用户未读消息数量
     */
    @Select("SELECT COUNT(*) FROM que_mailbox WHERE user_id = #{userId} AND is_read = 0")
    Integer countUnreadMessages(@Param("userId") Integer userId);

    /**
     * 查询用户消息列表
     */
    @Select("""
        SELECT * FROM que_mailbox 
        WHERE user_id = #{userId} 
        ORDER BY create_time DESC 
        LIMIT #{offset}, #{limit}
    """)
    List<QueMailboxEntity> selectUserMessages(@Param("userId") Integer userId, 
                                            @Param("offset") Integer offset, 
                                            @Param("limit") Integer limit);

    /**
     * 标记消息为已读
     */
    @org.apache.ibatis.annotations.Update("UPDATE que_mailbox SET is_read = 1 WHERE mailbox_id = #{mailboxId}")
    void markAsRead(@Param("mailboxId") Long mailboxId);
}
